package com.youhome.business.controller;

import com.youhome.business.dto.DTOCounselorCreate;
import com.youhome.business.dto.DTOCounselorUpdate;
import com.youhome.common.exception.CustomException;
import com.youhome.common.response.Result;
import com.youhome.common.response.ResultCode;
import com.youhome.common.response.ResultPage;
import com.youhome.service.model.BCounselor;
import com.youhome.service.model.Principal;
import com.youhome.service.service.BCompanyService;
import com.youhome.service.service.BCounselorService;
import io.swagger.annotations.*;
import org.modelmapper.ModelMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.security.core.annotation.AuthenticationPrincipal;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

/**
 * Created by chen365 on 2017/10/23.
 */
@RestController
@RequestMapping(value = "/counselor")
@Api(tags = "顾问")
public class CounselorController {
    @Autowired
    private BCounselorService bCounselorService;
    @Autowired
    private BCompanyService bCompanyService;

    @ApiOperation(value = "创建顾问", notes = "创建顾问", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    @PostMapping(value = "/create")
    public Object create(@ApiParam @Validated DTOCounselorCreate dtoCounselorCreate, @ApiIgnore @AuthenticationPrincipal Principal principal) throws CustomException {
        ModelMapper modelMapper = new ModelMapper();
        return new Result(ResultCode.SUCCESS,
                bCounselorService.create(principal, modelMapper.map(dtoCounselorCreate, BCounselor.class))
        );
    }

    @ApiOperation(value = "顾问列表", notes = "顾问列表", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    @ApiImplicitParams({
            @ApiImplicitParam(dataType = "int", name = "pageNumber", value = "页码", paramType = "query"),
            @ApiImplicitParam(dataType = "int", name = "pageSize", value = "每页条数", paramType = "query"),
            @ApiImplicitParam(dataType = "String", name = "keyword", value = "搜索关键字(name或者phone)", paramType = "query"),
            @ApiImplicitParam(dataType = "String", name = "istop", value = "选择置顶顾问（true/false）", paramType = "query")
    })
    @GetMapping(value = "/list")
    public Object list(Integer pageNumber, Integer pageSize, String keyword, Boolean istop,@ApiIgnore @AuthenticationPrincipal Principal principal) throws CustomException {
        return new ResultPage(ResultCode.SUCCESS, bCounselorService.select(principal,pageNumber, pageSize, keyword, istop));
    }


    @ApiOperation(value = "顾问列表", notes = "顾问列表", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    @GetMapping(value = "/list/select")
    public Object listForSelect(@ApiIgnore @AuthenticationPrincipal Principal principal) throws CustomException {
        return new Result(ResultCode.SUCCESS, bCounselorService.select(principal));
    }

    @ApiOperation(value = "获取顾问所属公司UUID", notes = "获取顾问所属公司UUID", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    @GetMapping(value = "/company/uuid")
    public Object companyUUID(@ApiIgnore @AuthenticationPrincipal Principal principal) throws CustomException {
        return new Result(ResultCode.SUCCESS, bCompanyService.selectUUID(principal));
    }

    @ApiOperation(value = "导出顾问列表", notes = "导出客户列表", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    @GetMapping(value = "/export")
    public Object export(@ApiIgnore @AuthenticationPrincipal Principal principal) throws CustomException {
        return new Result(ResultCode.SUCCESS, bCounselorService.export(principal));
    }

    @ApiOperation(value = "顾问详情", notes = "根据ID获取顾问详情", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    @ApiImplicitParams({
            @ApiImplicitParam(dataType = "Long", name = "counselorId", value = "顾问ID", paramType = "query")
    })
    @GetMapping(value = "/detail")
    public Object detail(Long counselorId, @ApiIgnore @AuthenticationPrincipal Principal principal) {
        return new Result(ResultCode.SUCCESS, bCounselorService.selectDetails(principal, counselorId));
    }

    @ApiOperation(value = "修改顾问", notes = "修改顾问", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    @PutMapping(value = "/update")
    public Object update(@ApiParam @Validated DTOCounselorUpdate dtoCounselorUpdate) throws CustomException {
        ModelMapper modelMapper = new ModelMapper();
        bCounselorService.update(modelMapper.map(dtoCounselorUpdate, BCounselor.class));
        return new Result(ResultCode.SUCCESS);
    }

    @ApiOperation(value = "删除顾问", notes = "删除顾问", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    @ApiImplicitParams({
            @ApiImplicitParam(dataType = "Long", name = "counselorId", value = "顾问ID", required = true, paramType = "query")
    })
    @DeleteMapping(value = "/delete")
    public Object delete(Long counselorId, @ApiIgnore @AuthenticationPrincipal Principal principal) throws CustomException {
        bCounselorService.delete(principal, counselorId);
        return new Result(ResultCode.SUCCESS);
    }

    @ApiOperation(value = "顾问置顶", notes = "顾问置顶", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    @ApiImplicitParams({
            @ApiImplicitParam(dataType = "Long", name = "counselorId", value = "顾问ID", required = true, paramType = "query")
    })
    @PutMapping(value = "/istop")
    public Object isTop(Long counselorId) throws CustomException {
        bCounselorService.top(counselorId);
        return new Result(ResultCode.SUCCESS);
    }

    @ApiOperation(value = "置顶顾问列表", notes = "置顶顾问列表", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    @ApiImplicitParams({
            @ApiImplicitParam(dataType = "String", name = "uuid", value = "公司UUID", required = true, paramType = "query"),
            @ApiImplicitParam(dataType = "int", name = "pageNumber", value = "页码", paramType = "query"),
            @ApiImplicitParam(dataType = "int", name = "pageSize", value = "每页条数", paramType = "query")
    })
    @GetMapping(value = "/istop/list")
    public Object isTopList(String uuid, Integer pageNumber, Integer pageSize) throws CustomException {
        return new ResultPage(ResultCode.SUCCESS, bCounselorService.selectTopByCompanyID(uuid, pageNumber, pageSize));
    }


    @ApiOperation(value = "修改顾问排序", notes = "修改顾问排序", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    @ApiImplicitParams({
            @ApiImplicitParam(dataType = "String", name = "uuid", value = "公司UUID", required = true, paramType = "query"),
            @ApiImplicitParam(dataType = "int", name = "sort", value = "排序", required = true, paramType = "query")
    })
    @PutMapping(value = "/sort")
    public Object sort(String uuid, Integer sort) throws CustomException {
        bCounselorService.updateSortByPrimaryKey(uuid, sort);
        return new Result(ResultCode.SUCCESS);
    }

    @ApiOperation(value = "查询置顶顾问数量", notes = "查询置顶顾问数量", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    @GetMapping(value = "/count")
    public Object count(@ApiIgnore @AuthenticationPrincipal Principal principal) throws CustomException {
        return new Result(ResultCode.SUCCESS, bCounselorService.selectCountByCompanyId(principal));
    }
}
